package zt.song.config;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;

import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import zt.song.domain.User;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 宋伟宁
 * @Date 2023/11/17
 * @Version 1.0
 **/
@Component
public class MySuccessHandler  implements AuthenticationSuccessHandler {

    private String url;

    public MySuccessHandler() {
    }

    public MySuccessHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        User user =(User) authentication.getPrincipal();
        System.out.println(user.getUsername());

        System.out.println(user.getPassword());
        System.out.println(user.getAuthorities());
        System.out.println("-----------------");
        Object details = authentication.getDetails();
        System.out.println(details);
        System.out.println(authentication.isAuthenticated());

//        response.setStatus(HttpServletResponse.SC_OK);
//        //设置响应头
//        response.setHeader("Content-Type","application/json;charset=utf-8");
//        PrintWriter writer = response.getWriter();
//        writer.write("{\"status\":\"OK\",\"msg\":\"登录成功\"  }");
//        writer.close();
         response.sendRedirect(this.url);
    }
}
